/******************************************************************************
 *  Project:		Neddle Community Edition
 *  Filename:		EnumExtensions.cs
 *	Created By:     Kristopher Cargile <kristopher@neddle.org>
 *  Date Created:	March 29, 2011
 * ****************************************************************************
 * IMPORTANT GNU LICENSE INFORMATION. DO NOT REMOVE THIS SECTION!
 * 
 * Neddle :: Copyright (c) 2010-2012, Cargile Technology Group, LLC
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ******************************************************************************/

using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;

namespace Neddle 
{
    /// <summary>
    /// EnumExtensions
    /// </summary>
    public static class EnumExtensions
    {
        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <param name="enumeration">The enumeration.</param>
        /// <returns></returns>
        public static string GetDescription(this Enum enumeration)
        {
            FieldInfo fi = enumeration.GetType().GetField(enumeration.ToString());
            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                                             typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : enumeration.ToString();
        }

        /// <summary>
        /// Gets the enumeration name.
        /// </summary>
        /// <param name="enumType">enumeration type</param>
        /// <param name="description">The description.</param>
        /// <returns></returns>
        public static string GetName(this Type enumType, string description)
        {
            FieldInfo[] fis = enumType.GetFields();
            foreach (FieldInfo fi in fis)
            {
                DescriptionAttribute[] attributes =
                  (DescriptionAttribute[])fi.GetCustomAttributes
                  (typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    if (attributes[0].Description == description)
                    {
                        return fi.Name;
                    }
                }
            }
            return description;
        }

        /// <summary>
        /// Gets the enum list.
        /// </summary>
        /// <param name="enumerationType">Type of the enumeration.</param>
        /// <returns></returns>
        public static List<KeyValuePair<Enum, string>> GetEnumList(this Type enumerationType)
        {
            if (enumerationType == null)
            {
                throw new ArgumentNullException("enumerationType");
            }

            //ArrayList list = new ArrayList();
            List<KeyValuePair<Enum, string>> list = new List<KeyValuePair<Enum, string>>();

            Array enumValues = Enum.GetValues(enumerationType);

            foreach (Enum value in enumValues)
            {
                list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
            }
            return list;
        }
    }
}